package com.parking.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.common.core.model.RedisKey;
import com.parking.common.core.util.DateUtil;
import com.parking.common.web.util.RedisUtil;
import com.parking.order.model.PlatformIncome;
import com.parking.order.dao.PlatformIncomeDao;
import com.parking.order.model.dto.findMonthlyIncomeAndHourPlatformDto;
import com.parking.order.service.IPlatformIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2023年11月28日
 */
@Service
public class PlatformIncomeServiceImpl extends ServiceImpl<PlatformIncomeDao, PlatformIncome> implements IPlatformIncomeService {
    /**
     * Redis工具类
     * */
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 平台收入数据层
     * */
    @Autowired
    private PlatformIncomeDao platformIncomeDao;
    /**
     * 平台查询12月每日收入
     * 返回 List<Integer> 1-31日 收入 BigDecimal->int
     * */
    @Override
    public List<Integer> findDailyIncomePlatform() {
        List<Integer> incomes = new ArrayList<>();
        for(int i=1;i<=31;i++){
            // 返回12月1日的收入 BigDecimal->Int
            String day = "2023-12-" + (i<10?"0"+i:i);
            PlatformIncome platform = platformIncomeDao.selectOne(new QueryWrapper<PlatformIncome>().eq("year_month_time",day));
            int income = platform==null?0:platform.getIncome().intValue();
            if(DateUtil.localDateToString(LocalDate.now()).equals(day)){
                // 当天还要算Redis
                if(redisUtil.exist(RedisKey.platformIncome())){
                    income += (int) Double.parseDouble(redisUtil.get(RedisKey.platformIncome()));
                }
            }
            incomes.add(income);
        }

        return incomes;
    }

    /**
     * 平台查询12月出租小时数与总收入
     * 参数 平台token authorization
     * 返回 hour 出租总小时数
     *     income 总收入
     * */
    @Override
    public findMonthlyIncomeAndHourPlatformDto findMonthlyIncomeAndHourPlatform() {
        PlatformIncome platform = platformIncomeDao.selectOne(new QueryWrapper<PlatformIncome>().eq("year_month_time","2023-12"));
        int income = platform==null?0:platform.getIncome().intValue();
        int hour = platform==null?0:platform.getYearMonthHour();
        if(redisUtil.exist(RedisKey.platformIncome())){
            income += (int) Double.parseDouble(redisUtil.get(RedisKey.platformIncome()));
        }
        if(redisUtil.exist(RedisKey.platformStallHour())){
            hour += (int) Double.parseDouble(redisUtil.get(RedisKey.platformIncome()));
        }
        return new findMonthlyIncomeAndHourPlatformDto(income,hour);
    }
}
